import reactive from './reactive.js';
import getDep from './getDep.js';

function createGetter() {
  return function (target, key) {
    const res = Reflect.get(target, key);
    const dep = getDep(target, key);
    // console.log(`get: ${key}`);
    dep.depend();
    return reactive(res);
  };
}

function createSetter() {
  return function (target, key, newVal) {
    if (newVal === target[key]) return true;
    const res = Reflect.set(target, key, newVal);
    const dep = getDep(target, key);
    // console.log(`set: ${key}`);
    dep.notify();
    return res;
  };
}

export const mutableHandler = {
  get: createGetter(),
  set: createSetter()
};
